<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品类型" prop="productKey" width="240px">
        <product-key-select v-model="queryParams.productKey"/>
      </el-form-item>

      <el-form-item label="设备编号" prop="deviceId">
        <el-input
            v-model="queryParams.deviceId"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备标识" prop="deviceCode">
        <el-input
            v-model="queryParams.deviceCode"
            placeholder="请输入设备编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="handleStatus">
        <el-select v-model="queryParams.handleStatus" placeholder="请选择处理状态" style="width: 200px" clearable>
          <el-option label="未处理" value=0 />
          <el-option label="已处理" value=1 />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmMessageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品key" align="center" prop="productKey"/>
      <el-table-column label="设备编号" align="center" prop="deviceId"/>
      <el-table-column label="设备标识" align="center" prop="deviceCode" width="150px"/>
      <el-table-column label="标题" align="center" prop="alarmTitle" width="150px"/>
      <el-table-column label="告警值" align="center" prop="alarmValue"/>
      <el-table-column label="事件级别" align="center" prop="alarmLevel">
        <template #default="scope">
          <el-tag v-if="scope.row.alarmLevel=== 0" type="info">低</el-tag>
          <el-tag v-else-if="scope.row.alarmLevel === 1" type="warning">中</el-tag>
          <el-tag v-else-if="scope.row.alarmLevel === 2" type="danger">高</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报警时间" align="center" prop="alarmTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="handleStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.handleStatus=== 0" type="danger">未处理</el-tag>
          <el-tag v-else-if="scope.row.handleStatus === 1" type="success">已处理</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="alarmContent" width="500px" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button
              link type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['manager:rfid:edit']"
          >修改</el-button>
          <el-button
              link type="danger"
              icon="Delete"
              v-hasPermi="['manager:productInfo:remove']"
              @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改事件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="alarmMessageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="alarmTitle">
          <el-input v-model="form.alarmTitle" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="产品key" prop="productKey">
          <el-input v-model="form.productKey" placeholder="请输入产品key"/>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备编号"/>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号"/>
        </el-form-item>
        <el-form-item label="字典值" prop="alarmProfileId">
          <el-input v-model="form.alarmProfileId" placeholder="请输入字典值"/>
        </el-form-item>
        <el-form-item label="告警值" prop="alarmValue">
          <el-input v-model="form.alarmValue" placeholder="请输入告警值"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.alarmContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="事件级别(0低 1中 2高)" prop="alarmLevel">
          <el-input v-model="form.alarmLevel" placeholder="请输入事件级别(0低 1中 2高)"/>
        </el-form-item>
        <el-form-item label="报警时间" prop="alarmTime">
          <el-date-picker clearable
                          v-model="form.alarmTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择报警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除标识,0:未删除,1:已删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除标识,0:未删除,1:已删除"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AlarmMessage">
import {
  listAlarmMessage,
  getAlarmMessage,
  delAlarmMessage,
  addAlarmMessage,
  updateAlarmMessage
} from "@/api/manager/alarmMessage";

import ProductKeySelect from "@/views/components/ProductKeySelect/index.vue";

const {proxy} = getCurrentInstance();

const alarmMessageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    alarmTitle: null,
    productKey: null,
    deviceId: null,
    deviceCode: null,
    alarmType: null,
    alarmProfileId: null,
    alarmValue: null,
    handleStatus: null,
    alarmContent: null,
    alarmLevel: null,
    alarmTime: null,
    deleted: null,
  },
  rules: {
    productKey: [
      {required: true, message: "产品key不能为空", trigger: "blur"}
    ],
    deviceCode: [
      {required: true, message: "设备编号不能为空", trigger: "blur"}
    ],
    alarmType: [
      {required: true, message: "属性值不能为空", trigger: "change"}
    ],
    alarmTime: [
      {required: true, message: "报警时间不能为空", trigger: "blur"}
    ],
    deleted: [
      {required: true, message: "逻辑删除标识,0:未删除,1:已删除不能为空", trigger: "blur"}
    ],
    createBy: [
      {required: true, message: "创建者不能为空", trigger: "blur"}
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
    updateBy: [
      {required: true, message: "更新者不能为空", trigger: "blur"}
    ],
    updateTime: [
      {required: true, message: "更新时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询事件列表 */
function getList() {
  loading.value = true;
  listAlarmMessage(queryParams.value).then(response => {
    alarmMessageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    alarmTitle: null,
    productKey: null,
    deviceId: null,
    deviceCode: null,
    alarmType: null,
    alarmProfileId: null,
    alarmValue: null,
    handleStatus: 0,
    alarmContent: null,
    alarmLevel: null,
    alarmTime: null,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("alarmMessageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
  console.log(queryParams.value)
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加事件";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getAlarmMessage(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改事件";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["alarmMessageRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAlarmMessage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAlarmMessage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除事件编号为"' + _ids + '"的数据项？').then(function () {
    return delAlarmMessage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manager/alarmMessage/export', {
    ...queryParams.value
  }, `alarmMessage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
